<?php

if (isset($_REQUEST['symbol']) && $_REQUEST['symbol'] != "") {

  $query = sprintf("
    SELECT stock.symbol, stock.name AS stock_name, exchange.name AS exchange_name
    FROM stock
    JOIN exchange ON stock.exchange_id = exchange.id
    WHERE stock.symbol = '%s'",
    mysql_real_escape_string($_REQUEST['symbol']));
  
  if (!($result = mysql_query($query)) || mysql_num_rows($result) == 0) {
    
    throw new Exception("The requested symbol \"". mysql_real_escape_string($_REQUEST['symbol']) ."\" could not be found.", 404);
  
  }
  
  $stock = mysql_fetch_assoc($result);
  
  mysql_free_result($result);
  
  $query = "
    SELECT symbol_1, symbol_2, value, s1.name as s1_name, s2.name as s2_name
    FROM correlation_data
    JOIN stock s1 ON correlation_data.symbol_1 = s1.symbol
    JOIN stock s2 ON correlation_data.symbol_2 = s2.symbol
    WHERE symbol_1 = \"". $stock['symbol'] ."\" OR
          symbol_2 = \"". $stock['symbol'] ."\"
    ORDER BY value DESC
    LIMIT 5"; 
   
   $positive = array();
   if (($result = mysql_query($query)) && mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_assoc($result)) {
        $positive[] = $row;
      }
      array_reverse($positive);
   }
   
   $query = "
    SELECT symbol_1, symbol_2, value, s1.name as s1_name, s2.name as s2_name
    FROM correlation_data
    JOIN stock s1 ON correlation_data.symbol_1 = s1.symbol
    JOIN stock s2 ON correlation_data.symbol_2 = s2.symbol
    WHERE symbol_1 = \"". $stock['symbol'] ."\" OR
          symbol_2 = \"". $stock['symbol'] ."\"
    ORDER BY value
    LIMIT 5";
   
   $negative = array();
   if (($result = mysql_query($query)) && mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_assoc($result)) {
        $negative[] = $row;
      }
      array_reverse($negative);
   }
  
} else {
  throw new Exception("Please enter a stock symbol.", 400);
}
  
$query = sprintf("
  SELECT date, adj_close
  FROM stock_data
  WHERE symbol = '%s'
  ORDER BY date",
  $stock["symbol"]);

$result = mysql_query($query);

?>
      <script type="text/javascript">
        google.load("visualization", "1", {packages:["corechart"]});
        google.setOnLoadCallback(drawChart);
        function drawChart() {
          var data = new google.visualization.DataTable();
          data.addColumn('string', 'Date');
          data.addColumn('number', 'Close');
          data.addRows([
<?php

  while ($row = mysql_fetch_assoc($result)) {
  
    echo "            ['" . $row['date'] ."', ". $row['adj_close'] ."],\n";
    
  }

?>
      
          ]);
      
          var options = {
            //width: 960, height: 240
            legend: 'none'
          };
      
          var chart = new google.visualization.LineChart(document.getElementById('main-chart'));
          chart.draw(data, options);
        }
      </script>
      
      <header>
        <div class="row">
          <div class="span6">
            <h1>
              <?php echo $stock["stock_name"]; ?> (<?php echo $stock["symbol"]; ?>)</h1>
            <h2>606.45 <span class="positive">+2.64 (+0.43%)</span></h2>
            <table class="table table-bordered table-striped">
              <tbody>
                <tr>
                  <td>Exchange</td>
      	          <td><?php echo $stock["exchange_name"]; ?></td>
                </tr>
                <tr>
                  <td>IPO</td>
      	          <td>????</td>
                </tr>
                <tr>
                  <td>Market cap</td>
      	          <td>????</td>
                </tr>
                <tr>
                  <td>P/E</td>
      	          <td>????</td>
                </tr>
              </tbody>
            </table>
          </div>
          <div class="span6">
            <h1>&nbsp;</h1>
            <div id="main-chart" />
          </div>
        </div>
      </header>
      <hr />
      <section id="details">
        <div class="row">
          <div class="span6">
            <h2>Strongest Direct Correlations</h2>
            <table class="table table-bordered table-striped">
              <tbody>
<?php $i = 1; ?>
<?php foreach ($positive as $pos) { ?>
                <tr>
                  <td><?php echo $i++; ?></td>
                  <td><span class="compositeline"></span>
                    <a href="/details?symbol=<?php echo ($pos['symbol_1'] == $stock['symbol']) ? $pos['symbol_2'] : $pos['symbol_1']; ?>">
                      <?php echo ($pos['symbol_1'] == $stock['symbol']) ? $pos['symbol_2'] : $pos['symbol_1']; ?>
                    </a>
                  </td>
                  <td>
                    <a href="/details?symbol=<?php echo ($pos['symbol_1'] == $stock['symbol']) ? $pos['symbol_2'] : $pos['symbol_1']; ?>">
                      <?php echo ($pos['symbol_1'] == $stock['symbol']) ? $pos['s2_name'] : $pos['s1_name']; ?>
                    </a>
                  </td>
                  <td class="<?php echo ($pos['value'] >= 0) ? "positive" : "negative"; ?>">
                    <?php echo $pos['value'] * 100; ?>%
                  </td>
                </tr>
<?php } ?>
                <tr>
                  <td colspan="3"><a href="#">View more...</a></td>
                </tr>
              </tbody>
            </table>
          </div><div class="span6">
            <h2>Strongest Inverse Correlations</h2>
            <table class="table table-bordered table-striped">
              <tbody>
<?php $i = 1; ?>
<?php foreach ($negative as $neg) { ?>
                <tr>
                  <td>
                    <?php echo $i++; ?>
                  </td>
                  <td>
                    <span class="compositeline"></span>
                    <a href="/details?symbol=<?php echo ($neg['symbol_1'] == $stock['symbol']) ? $neg['symbol_2'] : $neg['symbol_1']; ?>">
                    <?php echo ($neg['symbol_1'] == $stock['symbol']) ? $neg['symbol_2'] : $neg['symbol_1']; ?>
                    </a>
                  </td>
                  <td>
                    <a href="/details?symbol=<?php echo ($neg['symbol_1'] == $stock['symbol']) ? $neg['symbol_2'] : $neg['symbol_1']; ?>">
                    <?php echo ($neg['symbol_1'] == $stock['symbol']) ? $neg['s2_name'] : $neg['s1_name']; ?>
                    </a>
                  </td>
                  <td class="<?php echo ($neg['value'] >= 0) ? "positive" : "negative"; ?>">
                    <?php echo $neg['value'] * 100; ?>%
                  </td>
                </tr>
<?php } ?>
                <tr>
                  <td colspan="4"><a href="#">View more...</a></td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </section>